﻿using System.Collections.Generic;
using System.IO;
using System.Linq;

using CommonLibrary.ConfigurationManager.Helpers;
using CommonLibrary.Exceptions;
using CommonLibrary.Extensions;
using CommonLibrary.Models.Configurations;
using CommonLibrary.Models.Profiles;

namespace CommonLibrary.ConfigurationManager
{
    static public class ConfigManager
    {
        private const string ConfigFileName = "Config.xml";
        private const string ProfilePath = "Profiles/";
        
        #region Load Aplication profiles
        public static Config GetConfig(this Config config)
        {
            return XmlFileParser<Config>.ParseXmlFile(ConfigFileName);
        }

        public static void GetApplicationProfile(this ProcessedProfile processedProfile, string applicationName)
        {
            var filePaths = Directory.GetFiles(ProfilePath, "*.xml").ToList();
            var filePath = filePaths.FirstOrDefault(f => f.Contains(applicationName));

            if (filePath == null) throw new SimpleSmartException("Unable to find profile for: " + applicationName);

            ApplicationProfileConfig applicationProfile = XmlFileParser<ApplicationProfileConfig>.ParseXmlFile(filePath);
            
            processedProfile.GetProcessProfile(applicationName, applicationProfile);
        }

        public static List<string> GetAllApplicationProfiles()
        {
            var filePaths = Directory.GetFiles(ProfilePath, "*.xml").ToList();
            return filePaths.ConvertAll(Path.GetFileNameWithoutExtension);
        }
        #endregion

        #region Save Application Profiles

        #endregion

        // File Generation Methods
        /*
        public void GenerateProfileFile(string applicationName)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(ApplicationProfileConfig));

            string path = ProfilePath + applicationName + ".xml";
            if (!File.Exists(path)) File.Create(path);

            TextWriter writer = new StreamWriter(path);

            ProfileConfigGenerator profileGenerator = new ProfileConfigGenerator();
            ApplicationProfileConfig configFile = profileGenerator.GenerateNewProfile(applicationName);

            serializer.Serialize(writer, configFile);

            writer.Close();
            writer.Dispose();
        }

        // This methods should be removed once the config file stucture is advanced enought

        public void GenerateConfigFile()
        {
            XmlSerializer serializer = new XmlSerializer(typeof(Config));
            TextWriter writer = new StreamWriter(ConfigFileName);

            Config configFile = new Config();

            ComPortProp(ref configFile.SerialPortConfig);

            serializer.Serialize(writer, configFile);

            writer.Close();
            writer.Dispose();
        }

        private void ComPortProp(ref SerialPortConfig cfg)
        {
            cfg.PortName = "COM3";
            cfg.BaudRate = 500000;
            cfg.DataBits = 8;
            cfg.Parirty = Parity.None;
            cfg.DataMode = DataMode.Hex;
            cfg.StopBits = StopBits.One;
            cfg.ClearOnOpen = false;
            cfg.ClearWithDTR = false;
            cfg.ReadTimeOut = 50;
            cfg.WriteTimeOut = 50;
        }*/
    }
}
